﻿using LTP.Accounts;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnagr.Web.weatherinfo
{
    /// <summary>
    /// GetPicData 的摘要说明
    /// </summary>
    public class GetPicData : IHttpHandler, IRequiresSessionState
    {
        jnagr.BLL.pdatainfo bll = new BLL.pdatainfo();
        StringBuilder result = new StringBuilder();

        StringBuilder sb = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "query")
                Query(context);
            else if (ac == "queryp")
                Querypic(context);
            else if (ac == "querycz")
                Querycz(context);
            else if (ac == "querybz")
                Querybz(context);
            else if (ac == "add")
                add(context);            
            else if (ac == "modify")
                modify(context);            
            else if (ac == "delete")
                delete(context);
            else if (ac == "updateall")
                updateall(context);
            else if (ac == "updateallcbd")
                updateallcbd(context);
        }
        public void updateallcbd(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid!=0)
            {                
                string uidstr = context.Request.QueryString["ids"];
                string ybqidstr = "", toolsNums = "";
                int mypetid = 0;
                
                if (null != context.Request.QueryString["cbdid"])
                {
                    ybqidstr = context.Request.QueryString["cbdid"];

                }
                string pro = Web.Admin.myinfo.getPCC(context).Replace(",", "");
                jnagr.BLL.myplant mybll = new BLL.myplant();
                DataSet dsp = mybll.GetListv(" province like'" + pro + "%'");
                if (dsp != null && dsp.Tables[0].Rows.Count > 0)
                {
                    int.TryParse(dsp.Tables[0].Rows[0]["mpid"].ToString(),out mypetid);
                }
                jnagr.BLL.myharmful myhbll = new BLL.myharmful();
                DataSet ds = myhbll.GetListv(" province='" + pro + "'");
                //增加数据
                int ybqn = bll.updateCBDhw(uidstr, ybqidstr, ds, mypetid, uid,pro);
                if (ybqn > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void updateall(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["ids"];
                string ybqidstr = "", toolsNums = "";
                int mypetid = 0;
                if (null != context.Request.QueryString["ptstr"])
                {
                    toolsNums = context.Request.QueryString["ptstr"];
                    if (toolsNums == "")
                    {
                        context.Response.Write("");//返回给前台页面  
                        context.Response.End();
                        return;
                    }

                }
                else
                {
                    context.Response.Write("");//返回给前台页面  
                    context.Response.End();
                    return;

                }
                if (null != context.Request.QueryString["ybqid"])
                {
                    ybqidstr = context.Request.QueryString["ybqid"];

                }
                DateTime dt = DateTime.Now;
                string[] pictime = toolsNums.Split('_');
                if (pictime.Length > 2)
                    dt = DateTime.ParseExact(pictime[1], "yyyyMMddHHmmss", CultureInfo.InvariantCulture); ;
                int l = 0;
                int.TryParse(ConfigurationManager.AppSettings["ybqpictime"], out l);
                if (l == 0) l = 15;
                //增加数据
                int ybqn=bll.updateYBQhw(uidstr,ybqidstr, toolsNums, dt, l);
                if (ybqn>0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int uid = 0;
                string uidstr = context.Request.QueryString["pdid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);

                
                if (bll.Delete(uid))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        
        public void modify(HttpContext context)
        {
            if (Web.Admin.myinfo.isLogin(context))
            {
                context.Response.ContentType = "text/plain";
                string uname = context.Request.Form["pname"];
                string upwd = context.Request.Form["pnum"];
                int pnum = 0;
                int.TryParse(upwd, out pnum);
                
                int uid = 0;
                string uidstr = context.Request.Form["pdid"];
                if (uidstr != "")
                    uid = int.Parse(uidstr);
                jnagr.Model.pdatainfo model = new jnagr.Model.pdatainfo();
                model.pname = uname;
                model.pnum= pnum;               
                model.pdid = uid;                
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                
                    string ptstr = context.Request.Form["ptstr"];
                    string uname = context.Request.Form["pname"];
                    string upwd = context.Request.Form["pnum"];
                    int pnum = 0;
                    int.TryParse(upwd, out pnum);

                    jnagr.Model.pdatainfo model = new jnagr.Model.pdatainfo();
                    model.pname = uname;
                    model.pnum = pnum;
                    model.pmark = ptstr;                   
                    
                    int rid = bll.Add(model);
                    if (rid > 0)
                    {                        
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
               
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Querycz(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string ybqidstr = "", toolsNums="";
                int mypetid = 0;
                if (null != context.Request.Form["ptstr"])
                {
                    toolsNums = context.Request.Form["ptstr"];
                    if (toolsNums == "")
                    {
                        context.Response.Write("");//返回给前台页面  
                        context.Response.End();
                        return;
                    }

                }
                else
                {
                    context.Response.Write("");//返回给前台页面  
                    context.Response.End();
                    return;

                }
                if (null != context.Request.Form["ybqid"])
                {
                    ybqidstr = context.Request.Form["ybqid"];
                    
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.QueryString["rows"])
                {
                    pageRows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());
                }
                if (null != context.Request.QueryString["page"])
                {
                    page = int.Parse(context.Request.QueryString["page"].ToString().Trim());
                }
                if (null != context.Request.QueryString["sort"])
                {
                    order = context.Request.QueryString["sort"].ToString().Trim();
                }
                if (null != context.Request.QueryString["order"])
                {
                    sort = context.Request.QueryString["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();              
                
                strWhere.AppendFormat(" ptstr='{0}' ", toolsNums);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                if (count == 0 && ybqidstr!="")
                {
                    DateTime dt =DateTime.Now;
                    string[] pictime = toolsNums.Split('_');
                    if (pictime.Length > 2)
                        dt = DateTime.ParseExact(pictime[1], "yyyyMMddHHmmss", CultureInfo.InvariantCulture); ;
                    int l = 0;
                    int.TryParse(ConfigurationManager.AppSettings["ybqpictime"], out l);
                    if (l == 0) l = 15;
                    //增加数据
                    bll.getYBQhw(ybqidstr, toolsNums,dt,l);
                }
                count = bll.GetRecordCount(strWhere.ToString());
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);                
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }
        public void Querybz(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string ybqidstr = "", toolsNums = "";
                
                if (null != context.Request.Form["ptstr"])
                {
                    toolsNums = context.Request.Form["ptstr"];
                    if (toolsNums == "")
                    {
                        context.Response.Write("");//返回给前台页面  
                        context.Response.End();
                        return;
                    }

                }
                else
                {
                    context.Response.Write("");//返回给前台页面  
                    context.Response.End();
                    return;

                }                
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.QueryString["rows"])
                {
                    pageRows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());
                }
                if (null != context.Request.QueryString["page"])
                {
                    page = int.Parse(context.Request.QueryString["page"].ToString().Trim());
                }
                if (null != context.Request.QueryString["sort"])
                {
                    order = context.Request.QueryString["sort"].ToString().Trim();
                }
                if (null != context.Request.QueryString["order"])
                {
                    sort = context.Request.QueryString["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();

                strWhere.AppendFormat(" ptstr='{0}' ", toolsNums);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数 
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", mypet = "", ptype = "", toolsNums = "";
                int mypetid = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                //if (null != context.Request.Form["mypet"])
                //{//获取前台传来的值  
                //    mypet = context.Request.Form["mypet"].ToString().Trim();
                //    mypetid = int.Parse(mypet);
                //}
                //if (null != context.Request.Form["ptype"])
                //{//获取前台传来的值  
                //    ptype = context.Request.Form["ptype"].ToString().Trim();
                //}
                if (null != context.Request.Form["bpid"])
                    toolsNums = context.Request.Form["bpid"];
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" ptime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" ptime<='{0}' and ", EndTime);
                }
                //if (mypetid != 0)
                //{
                //    strWhere.AppendFormat(" mhid='{0}' and ", mypetid);
                //}
                //if (ptype != "所有" && ptype != "")
                //{
                //    strWhere.AppendFormat(" petStage ='{0}' and ", ptype);
                //}
                strWhere.AppendFormat(" bpid='{0}' ", toolsNums);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }

        public void Querypic(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", mypet = "", ptype = "", toolsNums = "";
                int mypetid = 0;
                if (null != context.Request.QueryString["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.QueryString["StartTime"].ToString().Trim();
                }
                if (null != context.Request.QueryString["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.QueryString["EndTime"].ToString().Trim();
                }                
                if (null != context.Request.QueryString["bpid"])
                    toolsNums = context.Request.QueryString["bpid"];
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.QueryString["rows"])
                {
                    pageRows = int.Parse(context.Request.QueryString["rows"].ToString().Trim());
                }
                if (null != context.Request.QueryString["page"])
                {
                    page = int.Parse(context.Request.QueryString["page"].ToString().Trim());
                }
                if (null != context.Request.QueryString["sort"])
                {
                    order = context.Request.QueryString["sort"].ToString().Trim();
                }
                if (null != context.Request.QueryString["order"])
                {
                    sort = context.Request.QueryString["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" ptime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" ptime<='{0}' and ", EndTime);
                }               
                strWhere.AppendFormat(" bpid='{0}' ", toolsNums);
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPagep(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCountp(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}